package hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pojo.Condition;
import pojo.Deals;
import pojo.Productcatalog;
import dao.DealsDAO;

@Repository("DealsDAO")
public class HibernateDealsDAO implements DealsDAO {

	private SessionFactory sessionFactory;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Transactional
	public List<Deals> getDealsList() {
		Query query = sessionFactory.getCurrentSession().createQuery("from Deals");
		return query.list();
	}
	@Transactional
	public Deals getDeals(String dealid) {
		Session session = sessionFactory.getCurrentSession();
        Productcatalog pc = null;
        if(dealid.equals("-1"))
        	return null;
        String hql = String.format("from Deals pc where pc.dealid= %s", dealid);
        Query query = session.createQuery(hql);

        return (Deals) query.uniqueResult(); 
	}
}
